package com.adou.d15;

import java.util.*;
import java.util.stream.Collectors;

public class StreamTest5 {
    public static void main(String[] args) {
        List<Student> stus = new ArrayList<>();
        Student stu1 = new Student("蜘蛛精", 26, 175.5);
        Student stu2 = new Student("蜘蛛精2", 26, 175.5);
        Student stu3 = new Student("紫霞", 23, 167.6);
        Student stu4 = new Student("白晶晶", 25, 169.0);
        Student stu5 = new Student("牛魔王", 35, 183.3);
        Student stu6 = new Student("牛夫人", 34, 168.5);
        Collections.addAll(stus, stu1,stu2,stu3, stu4, stu5, stu6);

        // 需求1：计算身高超过168有几个人
        long count = stus.stream().filter(s -> s.getHeight() > 168).count();
        System.out.println(count);
        System.out.println("==============================================");

        // 需求2：找出身高最高的学生对象并输出
        Student student = stus.stream().max(((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight()))).get();
        System.out.println(student);
        System.out.println("==============================================");

        // 需求3:找出身高最矮个子的学生对象并输出
        Student student2 = stus.stream().min(((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight()))).get();
        System.out.println(student2);

        System.out.println("==============================================");

        // 需求4 请找身高大于170对象，并放到一个新的集合中
        // 流只能收集一次
        List<Student> st = stus.stream().filter(a -> a.getHeight() > 170).collect(Collectors.toList());
        System.out.println(st);

        System.out.println("==============================================");
        Set<Student> st2 = stus.stream().filter(a -> a.getHeight() > 170).collect(Collectors.toSet());
        System.out.println(st2);

        System.out.println("==============================================");

        // 需求5 请找身高大于170对象，并把名字/身高放到一个新的集合中
        Map<String, Double> st3 = stus.stream()
                .filter(a -> a.getHeight() > 170)
                .distinct()
                .collect(Collectors.toMap(a->a.getName(), a->a.getHeight()));
        System.out.println(st3); //Duplicate key 蜘蛛精 (attempted merging values // .distinct()

        // 数组
        Object[] objects = stus.stream()
                .filter(a -> a.getHeight() > 170).toArray();
        System.out.println(Arrays.toString(objects));

        Student[] stuA = stus.stream()
                .filter(a -> a.getHeight() > 170).toArray(len->new Student[len]);
        System.out.println(Arrays.toString(stuA));

    }
}
